package com.autumn.zero.listing;

import java.util.List;
import java.util.function.Consumer;

import com.autumn.mybatis.criterion.EntityQuery;
import com.autumn.mybatis.criterion.Query;
import com.autumn.mybatis.mapper.IEntityMapper;
import com.autumn.util.AutoMapUtils;

/**
 * 输出表达式
 * 
 * @author 杨昌国 2018-12-13 18:12:31
 * @param <TEntity>
 * @param <TEntityMapper>
 * @param <TResult>
 */
public class AutumnOutputZeroListingExpression<TEntity, TEntityMapper extends IEntityMapper<TEntity>, TResult>
		implements IAutumnZeroListingExpression {
	private final TEntityMapper entityMapper;
	private final Class<TResult> resultClass;
	private final EntityQuery<TEntity> entityQuery;

	/**
	 * 
	 * @param entityClass
	 * @param entityMapper
	 * @param consumer
	 */
	public AutumnOutputZeroListingExpression(Class<TEntity> entityClass, TEntityMapper entityMapper,
			Class<TResult> resultClass, Consumer<EntityQuery<TEntity>> consumer) {
		this.entityMapper = entityMapper;
		this.resultClass = resultClass;
		this.entityQuery = new EntityQuery<>(entityClass);
		if (consumer != null) {
			consumer.accept(entityQuery);
		}
	}

	@Override
	public Query getQuery() {
		return this.entityQuery;
	}

	@Override
	public Class<?> getResultClass() {
		return this.resultClass;
	}

	@Override
	public int executeCount(Query query) {
		return this.entityMapper.countByWhere(query.builderSection());
	}

	@Override
	public List<TResult> executeResult(Query query) {
		List<TEntity> entitys = this.entityMapper.selectList(query.builderSection());
		List<TResult> items = AutoMapUtils.mapList(entitys, this.resultClass);
		return items;
	}
}
